package com.moxiao.yfjh.module.order.controller.app;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.module.member.controller.app.user.vo.AppTeamReqVo;
import com.moxiao.yfjh.module.member.service.user.MemberUserService;
import com.moxiao.yfjh.module.order.common.NoCommonUtil;
import com.moxiao.yfjh.module.order.controller.admin.brokeragewithdraw.vo.BlindBrokerageWithdrawCreateReqVO;
import com.moxiao.yfjh.module.order.controller.admin.brokeragewithdraw.vo.BlindBrokerageWithdrawExportReqVO;
import com.moxiao.yfjh.module.order.controller.admin.brokeragewithdraw.vo.BlindBrokerageWithdrawRespVO;
import com.moxiao.yfjh.module.order.controller.app.vo.AppWithdrawVo;
import com.moxiao.yfjh.module.order.controller.app.vo.AppWithdrawalReqVo;
import com.moxiao.yfjh.module.order.convert.brokeragewithdraw.BlindBrokerageWithdrawConvert;
import com.moxiao.yfjh.module.order.dal.dataobject.brokeragewithdraw.BlindBrokerageWithdrawDO;
import com.moxiao.yfjh.module.order.tradeRefund.brokeragerecord.BlindBrokerageRecordService;
import com.moxiao.yfjh.module.order.tradeRefund.brokeragewithdraw.BlindBrokerageWithdrawService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;

@Tag(name = "app - 分销中心")
@RestController
@RequestMapping("/order/brokerage/record")
@Validated
public class AppBlindBrokerageRecordController {

    @Resource
    private BlindBrokerageRecordService brokerageRecordService;
    @Resource
    private MemberUserService memberUserService;
    @Resource
    private BlindBrokerageWithdrawService brokerageWithdrawService;


    /**
     * 分销中心
     */
    @GetMapping("/getWithdrawal")
    @Operation(summary = "我的分销中心")
    @Parameter(name = "userId", description = "用户id", required = true)
    public CommonResult<AppWithdrawalReqVo> getBrokerageRecordPage(@RequestParam("userId") Long userId) {
        AppWithdrawalReqVo vo = brokerageRecordService.getWithdrawal(userId);
        return success(vo);
    }

    /**
     * 我的团队列表
     */
    @GetMapping("/getTeam")
    @Operation(summary = "分销中心-我的团队列表")
    @Parameter(name = "userId", description = "用户id", required = true)
    public CommonResult getTeam(@RequestParam("userId") Long userId) {
        List<AppTeamReqVo> list = memberUserService.getTeamList(userId);
        return success(list);
    }

    /**
     * 我的提现-发起提现
     */
    @PostMapping("/initiate/withdrawal")
    @Operation(summary = "分销中心-发起提现")
    public CommonResult<Long> createWithdrawal(@Valid @RequestBody AppWithdrawVo createWithdraw) {

        BlindBrokerageWithdrawCreateReqVO createReqVO = new BlindBrokerageWithdrawCreateReqVO();
        createReqVO.setUserId(createWithdraw.getUserId());
        createReqVO.setPrice(createWithdraw.getPrice());
        createReqVO.setType(createWithdraw.getType());
        createReqVO.setNo(NoCommonUtil.getNo());
        createReqVO.setStatus((byte) 0);
        //总金额
        BigDecimal balance = brokerageRecordService.getBalance(createWithdraw.getUserId());
        // 已提现金额
        BigDecimal withdrawn = brokerageRecordService.getWithdrawn(createWithdraw.getUserId());
        //提现中金额
        BigDecimal progress = brokerageRecordService.getWithdrawalProgress(createWithdraw.getUserId());
        BigDecimal fee = balance.subtract(withdrawn.add(progress).add(createWithdraw.getPrice()));
        createReqVO.setBalance(fee);
        createReqVO.setTotalPrice(fee);
        return success(brokerageWithdrawService.createBrokerageWithdraw(createReqVO));
    }

    /**
     * 我的提现-发起提现
     */
    @GetMapping("/getWithdrawalRecord")
    @Operation(summary = "分销中心-我的提现记录")
    public CommonResult<List<BlindBrokerageWithdrawRespVO>> getWithdrawalRecord(@RequestParam("userId") Long userId, @RequestParam("status") Byte status) {
        BlindBrokerageWithdrawExportReqVO pageReqVO = new BlindBrokerageWithdrawExportReqVO();
        pageReqVO.setUserId(userId);
        pageReqVO.setStatus(status);
        List<BlindBrokerageWithdrawDO> list = brokerageWithdrawService.getList(pageReqVO);
        List<BlindBrokerageWithdrawRespVO> volist = BlindBrokerageWithdrawConvert.INSTANCE.convertList(list);
        return success(volist);
    }
}
